﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

using JohnnySins.DAL;
using JohnnySins.DAL.Repositories;
using JohnnySins.Entities;
using JohnnySins.Code;
using System.Configuration;

namespace JohnnySins
{
    public partial class Member : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //111
            if (CurrentUsers.IsWrong(HttpContext.Current.User.Identity.Name))
            {
                LogLoginHelper.AddLogLogin(HttpContext.Current.User.Identity.Name, "", new EpochAnswer() { Error = "Duplicate login" });
                //Response.Redirect("~");
                //return;
            }

            if (ConfigurationManager.AppSettings["IsDevelopmentEnvironment"] == null)
            {
                if (Session["Start"] == null)
                    Response.Redirect("~/Default.aspx");

                string urlCurrent = Request.Url.ToString().ToLower();
                if (!urlCurrent.Contains("//www."))
                {
                    string newUrl = urlCurrent.Replace("http://", "http://www.").Replace("https://", "https://www.");
                    Response.Redirect(newUrl, false);
                }
            }
            
            FillNextLiveShow();
            SetActiveLink();

            lblMember.Text = HttpContext.Current.User.Identity.Name;
        }

        protected void FillNextLiveShow()
        {
            using (var repo = RepositoryFactory.ResolveRepository<NextLiveShowRepository>())
            {
                string content = "";
                List<NextLiveShow> lst = repo.FindActive().ToList();
                foreach (NextLiveShow item in lst)
                {
                    content += string.Format("<div class=\"live_show_schedule\" style=\"display:none;\"><li><span>{0}</span><span>{1}</span><span>{2}</span></li></div>", item.NextLiveShowName, item.NextLiveShowDate.ToString("d"), item.NextLiveShowDate.ToString("t"));
                }
                slider2.InnerHtml = content;
            }

        }

        private void SetActiveLink()
        {
            string namePage = Page.GetType().FullName;

            hlHome.Attributes["class"] = hlHome.Attributes["class"].Replace(" active", "");
            hlMyVideos.Attributes["class"] = hlMyVideos.Attributes["class"].Replace(" active", "");
            hlMyPhotosets.Attributes["class"] = hlMyPhotosets.Attributes["class"].Replace(" active", "");
            hlFitness.Attributes["class"] = hlFitness.Attributes["class"].Replace(" active", "");
            hlMyLife.Attributes["class"] = hlMyLife.Attributes["class"].Replace(" active", "");
            hlLiveChat.Attributes["class"] = hlLiveChat.Attributes["class"].Replace(" active", "");
            hlLiveShow.Attributes["class"] = hlLiveShow.Attributes["class"].Replace(" active", "");
                        
            if (namePage == "ASP.member_home_aspx")
            {
                hlHome.Attributes["class"] += " active";
            }
            else if (namePage == "ASP.member_myvideos_aspx")
            {
                hlMyVideos.Attributes["class"] += " active";
            }
            else if ((namePage == "ASP.member_myphotosets_aspx") || (namePage == "ASP.member_photos_aspx"))
            {
                hlMyPhotosets.Attributes["class"] += " active";
            }
            else if (namePage == "ASP.member_livechat_aspx")
            {
                hlLiveChat.Attributes["class"] += " active";
            }
            else if (namePage == "ASP.member_liveshow_aspx")
            {
                hlLiveShow.Attributes["class"] += " active";
            }
            else if (namePage == "ASP.member_mylife_aspx")
            {
                hlMyLife.Attributes["class"] += " active";
            }
            else if (namePage == "ASP.member_fitness_aspx")
            {
                hlFitness.Attributes["class"] += " active";
            }   
        }   
    }
}